package com.zzyy.study.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyy.study.dto.StudentDTO;
import com.zzyy.study.entities.Student;
import com.zzyy.study.mq.sender.IMsgSender;
import com.zzyy.study.service.StudentService;
import com.zzyy.study.mapper.StudentMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* @author 阳
* @description 针对表【student(用户表)】的数据库操作Service实现
* @createDate 2024-06-16 14:03:03
*/
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService
{
    @Resource
    private IMsgSender msgSender;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String register(StudentDTO studentDTO)
    {
        Student student = new Student();
        student.setId(IdUtil.fastSimpleUUID());
        student.setName(studentDTO.getName());
        student.setAge(studentDTO.getAge());
        //1 入库
        this.save(student);
        //2 发送发送单条消息到MQ
        this.msgSender.sendSingleton(student);

        return student.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String registerError(StudentDTO studentDTO) {
        Student student = new Student();
        student.setId(IdUtil.fastSimpleUUID());
        student.setName(studentDTO.getName());
        student.setAge(studentDTO.getAge());

        //1 入库
        this.save(student);
        //2 发送发送单条消息到MQ
        this.msgSender.sendSingleton(student);
        //3 故意使坏
        int age = 10/0;
        return null;
    }
}




